#include "graphicsiplimgobject.h"
#include <QPainter>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QList>

GraphicsIplImgObject::GraphicsIplImgObject(int width, int height, QGraphicsObject *parent) :
        QGraphicsObject(parent), m_ImageWidth(width), m_ImageHeight(height)
{
    /*Init basic attribute*/
    m_Width = m_ImageWidth + EDGEWIDTH + EDGEWIDTH;
    m_Height = m_ImageHeight + EDGEHEIGHT + EDGEHEIGHT;
    m_CurrentTimeStamp = 0;

    /*Init IplQImage*/
    m_OrigImageData = cvCreateImage(cvSize(m_ImageWidth,m_ImageHeight), IPL_DEPTH_8U, 1);
    cvZero(m_OrigImageData);
    m_ImageData = new IplQImage(m_OrigImageData, 1);

    /*Init timestamp*/
    m_CurrentTimeStamp = PFCMU::get_timestamp(m_OrigImageData->imageData);

    /*Init titles*/
    m_TopTitle<<" "<<" "<<" ";
    m_BottomTitle<<" "<<" "<<" ";

    m_IsThumb = false;
}

QRectF GraphicsIplImgObject::boundingRect() const
{
    return QRectF(0,0,m_Width,m_Height);
}

QPainterPath GraphicsIplImgObject::shape() const
{
    QPainterPath path;
    path.addRect(0,0,m_Width,m_Height);
    return path;
}

void GraphicsIplImgObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    if(m_ImageData != NULL && m_OrigImageData != NULL)
    {
        if(m_IsThumb)
        {
            //thumb gray
            IplImage *rgbgray = cvCreateImage(cvSize(m_OrigImageData->width, m_OrigImageData->height),
                                              IPL_DEPTH_8U,3);
            cvCvtColor(m_OrigImageData,rgbgray,CV_GRAY2RGB);
            m_ImageData->updateImage(rgbgray,3);
            cvReleaseImage(&rgbgray);
        }
        else
        {
            m_ImageData->updateImage(m_OrigImageData,1);
        }
        QImage *qImg = m_ImageData->getQImage();
        painter->drawImage(EDGEWIDTH, EDGEHEIGHT, *qImg);
        m_CurrentTimeStamp = PFCMU::get_timestamp(m_OrigImageData->imageData);
        m_BottomTitle[m_BottomTitle.size()-1] = tr("Timecode: %1").arg(m_CurrentTimeStamp);
        painter->drawText(0,EDGEHEIGHT,m_TopTitle.join(tr(" ")));
        painter->drawText(0,m_Height,m_BottomTitle.join(tr(" ")));
    }
}

void GraphicsIplImgObject::setTitle(int vPos, int hPos, std::string title)
{
    int top_or_bottom = 0;
    if(vPos>1)
    {
        top_or_bottom = 1;
    }
    else if(vPos<0)
    {
        top_or_bottom = 0;
    }
    else
    {
        top_or_bottom = vPos;
    }
    int left_or_right = 0;
    if(top_or_bottom == 0)
    {
        //top
        if(hPos > m_TopTitle.size()-1)
        {
            left_or_right = m_TopTitle.size()-2;
        }
        else if(hPos<0)
        {
            left_or_right = 0;
        }
        else
        {
            left_or_right = hPos;
        }
        m_TopTitle[left_or_right] = tr(title.c_str());
    }
    else
    {
        //bottom
        if(hPos > m_BottomTitle.size()-1)
        {
            left_or_right = m_BottomTitle.size()-2;
        }
        else if(hPos<0)
        {
            left_or_right = 0;
        }
        else
        {
            left_or_right = hPos;
        }
        m_BottomTitle[left_or_right] = tr(title.c_str());
    }
}

void GraphicsIplImgObject::setTopTitle(int hPos, std::string title)
{
    this->setTitle(0,hPos,title);
}

void GraphicsIplImgObject::setBottomTitle(int hPos, std::string title)
{
    this->setTitle(1,hPos,title);
}

int GraphicsIplImgObject::getWidth() const
{
    return m_Width;
}

int GraphicsIplImgObject::getHeight() const
{
    return m_Height;
}

int GraphicsIplImgObject::getImageWidth() const
{
    return m_ImageWidth;
}

int GraphicsIplImgObject::getImageHeight() const
{
    return m_ImageHeight;
}

PFCMU::timestamp_t GraphicsIplImgObject::getCurrentTimeStamp() const
{
    return m_CurrentTimeStamp;
}

void GraphicsIplImgObject::setThumb()
{
    m_IsThumb = true;
}

IplImage *GraphicsIplImgObject::getRGBIplImage() const
{
    if(m_ImageData != NULL)
    {
        return m_ImageData->getIpl();
    }
}

IplImage *GraphicsIplImgObject::getOrigIplImage() const
{
    if(m_OrigImageData != NULL)
    {
        return m_OrigImageData;
    }
}

int GraphicsIplImgObject::getCurrentCam() const
{
    return m_CurrentCam;
}

void GraphicsIplImgObject::setCurrentCam(int num)
{
    m_CurrentCam = num;
    char camtitle[64];
    snprintf(camtitle, sizeof(camtitle), "Cam%d", m_CurrentCam);
    std::string str(camtitle);
    this->setTopTitle(1,str);
}

void GraphicsIplImgObject::forceUpdate() const
{
    this->scene()->views().at(0)->repaint();
}

void GraphicsIplImgObject::onReceivedImg()
{
    this->update(0,0,m_Width,m_Height);
}

void GraphicsIplImgObject::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseDoubleClickEvent(event);
    update();
    emit objectDoubleClicked(m_CurrentCam);
}
